#'
#' various spatial imexplicit species diversity indexes and stand structure diversity
#'
#' @aliases species_diversity shannon_diversity_index simpson_diversity_index PIE species_evenness
#'
#'@usage
#'species_diversity(com,indexes==c("shannon","simpson","PIE","evenness"))
#'shannon_diversity_index(spab)
#'simpson_diversity_index(spab)
#'PIE(spab)
#'species_evenness(spab)
#'stand_structure_diversity(com,dbh_break=NULL)
#'
#'@details
#' The shannon diversity index is defined as
#' 
#' H^'=-sum_\{i=1\}^S\{p_i ln(p_i)\}
#'
#'where p_i is the proportion of the complete assemblage represented by species i (p_i=N_i/N). The more species in the assemblage
#'and the more even their relateive abundances, the larger H^' will be. Like species richness, it is sensitive to the number of 
#'individuals and samples collected, and they do not always have good statistical performance.
#'
#'An exception is Simpson's (1949) index of concentration:
#'
#'D=1-sum_{i=1}^S{p_i^2}
#'
#'This index measures the probability that two randomly chosen individuals represent the same species. The higher this index is, 
#'the greater the diversity.
#'
#'For small sample size, an adjustment can be added to D to get the PIE
#'
#'PIE=n/(n-1)D
#'
#'where n is the total number of sampled individuals.
#'
#'Species evenness is defined as
#'
#'E=H^'/ln(S)
#'
#'where S is the total number of species in all of the samples.
#'
#'
#'@examples
#'data(BCI)
#'
#'spab=species_abundance(BCI)
#'
#'shannon_diversity_index(spab)
#'
#'simpson_diversity_index(spab)
#'
#'PIE(spab)
#'
#'species_evenness(spab)
#'
#'species_diversity(BCI)
#'
#' com=rcommunity(splist=1:10,abundance=c(11:20),traits=data.frame(dbh=rnorm(155,mean=5)))
#' 
#' stand_structure_diversity(com,dbh_break=seq(0.1,10,length.out=10))
#'
#'
#' @export
stand_structure_diversity=function(com,dbh_break=NULL){
  dbh_class=table(cut(com$dbh,dbh_break,include.lowest = T))
  #remove groups without any individual
  dbh_class=as.numeric(dbh_class[dbh_class!=0])
  #convert to proportional data
  dbh_class=dbh_class/sum(dbh_class)
  dbh_diversity=-sum(dbh_class*log(dbh_class))
  
  basal_area=tapply(com$dbh^2*pi/4,com$species,sum)
  basal_area=basal_area/sum(basal_area)
  ba_diversity=-sum(basal_area*log(basal_area))
  
  return(c("DBH_diversity"=dbh_diversity,"Basal_area_diversity"=ba_diversity))
}


#' @export
species_diversity=function(com,indexes=c("shannon","simpson","PIE","evenness")){
  
  re=numeric()
  spab=species_abundance(com)
  if(any(is.na(spab)))
    warning("There are missing values in the species abundance data")
  if("shannon" %in% indexes){
    re=c(re,shannon_diversity_index(spab))
  }
  if("simpson" %in% indexes){
    re=c(re,simpson_diversity_index(spab))
  }
  if("PIE" %in% indexes){
    re=c(re,PIE(spab))
  }
  if("evenness" %in% indexes){
    re=c(re,species_evenness(spab))
  }
  names(re)=indexes
  return(re)
}

#' @export
shannon_diversity_index=function(spab){
  p=spab/sum(spab)
  return(-sum(p*log(p)))
}

#' @export
simpson_diversity_index=function(spab){
  p=spab/sum(spab)
  return(1-sum(p^2))
}

#' @export
PIE=function(spab){
  p=spab/sum(spab)
  n=sum(spab)
  return(n/(n-1)*(1-sum(p^2)))
}

#' @export
species_evenness=function(spab){
  return(shannon_diversity_index(spab)/log(length(spab)))
}